//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#ifndef READ3DS_H
#define READ3DS_H

#include "m5d.h"
#include "memfile.h"

//==================================================================
///
//==================================================================
class Read3DS
{
	PUtils::Memfile	*_memfp;
	int				_gv_chunk;
	M5D::Scene		*_scenep;
	M5D::Mesh		*_cur_meshp;
	M5D::Model		*_root_modelp;

	//==================================================================
	class ModelUserData
	{
	public:

		int	_node_id;
		int	_parent_node_id;

		ModelUserData( int node_id, int parent_node_id ) :
			_node_id(node_id),
			_parent_node_id(parent_node_id)
		{
			
		}
	};

	//==================================================================
	struct chunk
	{
		int	start;
		int	tag;
		int	length;
		int	end;
	};

public:
	Read3DS( M5D::Scene *scenep, PUtils::Memfile &memf, M5D::Model *root_modelp=NULL );
	M5D::Mesh *DoRead() throw(...);

private:
	char *read_string( char *s, int max );
	inline float read_float();
	inline void read_float3( float *dp );
	inline void read_xzy( float *dp );
	inline int read_int();
	inline u_short read_ushort(void);
	void chunk_begin( chunk *c );
	void chunk_end( chunk *c );
	void parse_colour( PMath::Vector3 &col );
	float parse_percentage(void);
	void parse_msh_mat_group( M5D::Triangle *trigsp ) throw(...);
	void parse_face_array( chunk *fc );
	void parse_point_array();
	void parse_tex_verts();
	void parse_mesh_matrix();
	void parse_n_tri_object( chunk *fc ) throw(...);
	void parse_named_object( chunk *fc ) throw(...);
	void parse_texmap( chunk *fc, M5D::Material *matep );
	void parse_mat_entry( chunk *fc );
	void parse_mdata( chunk *fc ) throw(...);
	void parse_3ds( chunk *fc ) throw(...);
	void parse_object_node( chunk *fc );
	void parse_kfdata( chunk *fc );
};


#endif
